#include "CEUtilities.h"

#include "CEGUIRadioBoxManager.h"


CEGUIRadioBoxManager * CEGUIRadioBoxManager::c_pCEGUIRadioBoxManager;

CEGUIRadioBoxManager* CEGUIRadioBoxManager::GetSingleton()
{
	if (!c_pCEGUIRadioBoxManager)
	{
		c_pCEGUIRadioBoxManager = new CEGUIRadioBoxManager();
	}

	return c_pCEGUIRadioBoxManager;
}

bool CEGUIRadioBoxManager::AddNewGroup(string groupname)
{
	if(groupname != "")
	{
		CEGUIRadioBoxGroup* radioboxgroup = new CEGUIRadioBoxGroup(groupname);
		m_pRadioGroupVector.push_back(radioboxgroup);
		return true;
	}
	else
		return false;
}

CEGUIRadioBoxGroup* CEGUIRadioBoxManager::GetGroup(string groupname)
{
	int size = m_pRadioGroupVector.size();
	for(int i = 0; i < size; ++i)
	{
		if(m_pRadioGroupVector[i]->GetGroupName() == groupname)
			return m_pRadioGroupVector[i];
	}

	return NULL;
}

bool CEGUIRadioBoxManager::DeleteGroup(string groupname)
{
	vector<CEGUIRadioBoxGroup*>::iterator p;
	for(p = m_pRadioGroupVector.begin();p != m_pRadioGroupVector.end();++p)
	{
		if((*p)->GetGroupName() == groupname)
		{
			delete (*p);
			m_pRadioGroupVector.erase(p);
			return true;
		}
	}
	
	return false;
}


bool CEGUIRadioBoxManager::RemoveElement(string groupname, int elementid)
{
	CEGUIRadioBoxGroup* g = GetGroup(groupname);
	if (!g) return false;
	
	int elementnum = g->RemoveElement(elementid);
	if(elementnum == 0)
		DeleteGroup(groupname);

	return true;
}

bool CEGUIRadioBoxManager::DeleteElement(string groupname, int elementid)
{
	int elementnum = GetGroup(groupname)->DeleteElement(elementid);
	if(elementnum == 0)
		DeleteGroup(groupname);

	return true;
}

bool CEGUIRadioBoxManager::SetElementToGroup(string groupname, CEGUIRadioBox* radiobox)
{
	CEGUIRadioBoxGroup*	oldgroup = GetGroup(radiobox->GetGroupName());
	CEGUIRadioBoxGroup* group = GetGroup(groupname);

	if(oldgroup != NULL)
	{
		if((group != NULL) && (group->GetElement(radiobox->GetID()) == NULL))
		{
			group->AddElement(radiobox);
			int elementnum = oldgroup->RemoveElement(radiobox->GetID());
			if(elementnum == 0)
				DeleteGroup(oldgroup->GetGroupName());
			return true;
		}
		else if(group == NULL)
		{
			AddNewGroup(groupname);
			GetGroup(groupname)->AddElement(radiobox);
			int elementnum = oldgroup->RemoveElement(radiobox->GetID());
			if(elementnum == 0)
				DeleteGroup(oldgroup->GetGroupName());
			return true;
		}
		
		return false;
	}
	else
	{
		if((group != NULL) && (group->GetElement(radiobox->GetID()) == NULL))
		{
			group->AddElement(radiobox);
			return true;
		}
		else if(group == NULL)
		{
			AddNewGroup(groupname);
			GetGroup(groupname)->AddElement(radiobox);
			return true;
		}
		
		return false;
	}
}

bool CEGUIRadioBoxManager::SetElementToDefault(string groupname, int elementid)
{
	return GetGroup(groupname)->SetElementToDefault(elementid);
}


HRESULT CEGUIRadioBoxManager::UpdateGroup(string groupname, int checkedID)
{
	return GetGroup(groupname)->Update(checkedID);
}